package com.spring.hot;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * @author Cizai
 * @since 2022/12/10 10:50
 **/
public class 字母异位词分组 {

    public List<List<String>> groupAnagrams(String[] strs) {

        final HashMap<String, List<String>> hashMap = new HashMap<>();

        for (String str : strs) {
            final char[] chars = str.toCharArray();
            Arrays.sort(chars);
            final String s = String.valueOf(chars);
            if (!hashMap.containsKey(s)) {
                hashMap.put(s,new ArrayList<>());
            }
            hashMap.get(s).add(str);
        }
        return new ArrayList<>(hashMap.values());

    }

}
